﻿using INTEX.DataBase.Tools;
using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Linq;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{
    public partial class ITXEXRPT
    { 
         
        private void TSTVV05(DataSet db)  
        {
            try
            {
                DataTable tbVVLIEF = dsSource.Tables["VVLIEF"];
                DataRow dwVV = tbVVLIEF.Rows[0];
                string src = "",str="";
                src = dwVV["VVNAME"].ToString().Trim();
                if (src == "")
                {
                    src = dwVV["DELNAME"].ToString().Trim();
                    if (src == "")
                    {
                        src = dwVV["CUSTOMER"].ToString().Trim();
                    }
                }
                exAgent.SetCellValue(7, 5, Helper.GetGB2312Value(src).Trim());
                src = dwVV["VVADR"].ToString().Trim();
                if (src == "")
                {
                    src = dwVV["DELADR"].ToString().Trim();
                    if (src == "")
                    {
                        src = dwVV["CUSTADR"].ToString().Trim();
                    }
                }
                exAgent.SetCellValue(8, 5, Helper.GetGB2312Value(src).Trim());
                src = dwVV["VVSTAFF"].ToString().Trim();
                if (src == "")
                {
                    src = dwVV["DELSTAFF"].ToString().Trim();
                    if (src == "")
                    {
                        src = dwVV["CUSTAFF"].ToString().Trim();
                    }
                }
                exAgent.SetCellValue(9, 5, Helper.GetGB2312Value(src).Trim());
                src = dwVV["VVTEL"].ToString().Trim();
                if (src == "")
                {
                    src = dwVV["DELTEL"].ToString().Trim();
                    if (src == "")
                    {
                        src = dwVV["CUTEL"].ToString().Trim();
                    }
                }
                src = src.Replace(":", "/");
                if (src.Trim().Length > 1)
                {
                    if (src.Substring(src.Length - 1, 1) == "/")
                    {
                        src = src.Substring(0, src.Length - 1);
                    }
                }
                exAgent.SetCellValue(10, 5, Helper.GetGB2312Value(src).Trim());

                src = dwVV["NAM"].ToString().Trim();
                exAgent.SetCellValue(7, 25, Helper.GetGB2312Value(src).Trim());
                src = dwVV["TEL"].ToString().Trim();
                exAgent.SetCellValue(8, 25, Helper.GetGB2312Value(src).Trim());
                src = dwVV["MOB"].ToString().Trim();
                exAgent.SetCellValue(9, 25, Helper.GetGB2312Value(src).Trim());
                src = dwVV["MAIL"].ToString().Trim();
                exAgent.SetCellValue(10, 25, Helper.GetGB2312Value(src).Trim());


                src = dwVV["VVLIEFSCH"].ToString();
                exAgent.SetCellValue(14, 5, System.Convert.ToInt64(src));
                src = dwVV["ORDERNO"].ToString();
                exAgent.SetCellValue(14, 14, src);
                src = dwVV["CUPO"].ToString().Trim();
                exAgent.SetCellValue(14, 24, Helper.GetGB2312Value(src).Trim());
                src = dwVV["VVVERSDAT"].ToString().Trim();//dwVV["VKKNEUDAT"].ToString().Trim();
                exAgent.SetCellValue(14, 28, Helper.GetGB2312Value(src).Trim());
                src = dwVV["CURRENCY"].ToString().Trim();
                exAgent.SetCellValue(15, 28, "总金额 (" + Helper.GetGB2312Value(src).Trim() + ")");
                exAgent.SetCellValue(15, 26, "单价 (" + Helper.GetGB2312Value(src).Trim() + ")");

                int x = 17, i = 1,pi = 0;
                decimal price=0,dis=0,totqty=0,totvalue=0;                  
                string unit = "",kdart="",kdcolor="";
                DataTable tbVVT = dsSource.Tables["VVTLIEF"];
                DataTable tbWB = dsSource.Tables["WBBST"];
                foreach (DataRow dwVVT in tbVVT.Rows)
                {
                    exAgent.SetCellValue(x, 1, System.Convert.ToInt64(i)); 
                    i = i + 1;
                    src = dwVVT["SUPP"].ToString().Trim();
                    exAgent.SetCellValue(x, 3, Helper.GetGB2312Value(src));
                    src = Helper.GetGB2312Value(dwVVT["VKPART"].ToString().Trim());
                    exAgent.SetCellValue(x, 8, src);
                    src = Helper.GetGB2312Value(dwVVT["VKPFARBE"].ToString().Trim());
                    exAgent.SetCellValue(x, 17, src);
                    unit = dwVVT["VKPMEEH"].ToString().Trim();
                    src = Helper.GetGB2312Value(unit);
                    exAgent.SetCellValue(x, 25, src);
                    src = dwVVT["PRICE"].ToString().Trim();
                    price = System.Convert.ToDecimal(src);
                    exAgent.SetCellValue(x, 26,price);
                    src = dwVVT["DIS"].ToString().Trim();
                    dis = System.Convert.ToDecimal(src);
                    exAgent.SetCellValue(x, 27, dis);
                    kdart = Helper.GetGB2312Value(dwVVT["VKPKDARTNR"].ToString().Trim());
                    kdcolor = Helper.GetGB2312Value(dwVVT["VKPKDFARBNR"].ToString().Trim());
                    if(kdart!=""||kdcolor!="")
                    {                      
                        exAgent.SetCellValue(x + 1, 7, "客户品种/颜色:");
                        exAgent.SetCellValue(x + 1, 8, kdart);
                        exAgent.SetCellValue(x + 1, 17, kdcolor);
                    }
                   
                    int J=0;                     
                    decimal qty=0,val=0,bqty = 0,bval=0;
                    var WB = from DataRow row in tbWB.AsEnumerable()
                             where row["WBBAUFKRS"].ToString() == dwVVT["VKPAUFKRS"].ToString()
                             && row["WBBAUFLFD"].ToString() == dwVVT["VKPAUFLFD"].ToString()
                             && row["WBBAUFPOS"].ToString() == dwVVT["VKPAUFPOS"].ToString()
                             select row;
                    foreach (DataRow row in WB)
                    {   
                        src = row["QTY"].ToString();
                        qty = System.Convert.ToDecimal(src);
                        exAgent.SetCellValue(x, 24, qty);
                        val = qty*price;
                        exAgent.SetCellValue(x, 28, val);
                        bqty = bqty+qty;
                        bval = bval+val;
                        x=x+1;
                        J=J+1;
                    }
                    bval = bval+dis;
                    pi=pi+J;
                    if(J<2)
                    {
                       if(kdart!=""||kdcolor!="")
                       {
                           x=x+1;
                       }
                         
                    }
                    
                    exAgent.SetCellValue(x, 17, "小计:");
                    exAgent.SetCellValue(x, 20, J.ToString()+" 卷");                           
                    exAgent.SetCellValue(x, 24, bqty);                          
                    exAgent.SetCellValue(x, 25, unit);                        
                    exAgent.SetCellValue(x, 28, bval);
                    x=x+2;
                    totqty = totqty+bqty;
                    totvalue = totvalue+bval;    
                }
                
                exAgent.SetCellValue(x, 17, "总计:");
                exAgent.SetCellValue(x, 20, pi.ToString() + " 卷");
                exAgent.SetCellValue(x, 24, totqty);
                exAgent.SetCellValue(x, 25, unit);
                exAgent.SetCellValue(x, 28, totvalue);
                    

                
                               
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);

            }            
        }
               
    }
}
